/***************************************************************************************************
* Filename: CmdManage.h
* Programmer: ou.cg
* Created: 2009.10.21
* Description:interface for the CCmdManger class.
* Note: 
*
************************************************************************************************/
#if !defined(AFX_CMDMANAGE_H__B90B992F_AEA4_4821_8876_1D038E879244__INCLUDED_)
#define AFX_CMDMANAGE_H__B90B992F_AEA4_4821_8876_1D038E879244__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

//////////////////////////////////////////////////////////////////////////
//#include "Function.h"
#include "CmdManageCom.h"
#include "SerialPort.h"
#include "CMD_def.h"
#include <Afxtempl.h>

//////////////////////////////////////////////////////////////////////////
class CCmdManage
{
public:
	CCmdManage();
	virtual ~CCmdManage();

public:
	void InitCmd(int Index,CString CMD,short Type,short ReadOnly,double Value,double DefValue,short Para);
	void InitCmdList();
	
	short ConnectToDevice(short iPort,int baud, int databits,int stopbits,int parity);
	short DisConnect();
	short SwitchToAxis(int iIndex);	

	//General communication command
	short GetCmdInt(short Index,short param,short *Value);
	short GetCmdUInt(short Index,unsigned short *Value);
	short GetCmdLong(short Index,long *Value);
	short GetCmdULong(short Index,unsigned long *Value);
	short GetCmdDouble(short Index,double *Value);
	short GetCmdString(short Index,char *Str,short Len);

	short SetCmd(short Index);
	short SetCmdInt(short Index,short param,short Value);
	short SetCmdUInt(short Index,unsigned short Value);
	short SetCmdLong(short Index,long Value);
	short SetCmdULong(short Index,unsigned long Value);
	short SetCmdDouble(short Index,double Value);
	short SetCmdString(short Index,char *Str,short Len);

	short RecOff();
	short RecDone(short *iDone);
	short GetRecord(double *fCHA,double *fCHB,double *fCHC,short *iRLen,short iPRLen,short *iFlag);
	short RecTrig(char *sCMDStr);
	short Record(char *sCMDStr);
	short ReadOneLine(char *parseStr,char *rtnStr,int iStartPos,int *iEndPos);

	CString ReadDataToString(short Length);
	short WriteStringToPort(char *cmdString);

	unsigned short Crc16(unsigned char* data , unsigned char length);
	short Read16bit(short iAxisNo, unsigned short iAddr, short *iVal);
	short Write16bit(short iAxisNo, unsigned short iAddr, short iVal);

	short WriteCmd10(short iAxisNo, unsigned short iAddr, long iVal);

public:

	TCommand CmdList[MAX_COMMAND_NUM];
    CArray<CString,CString> m_ErrorCmdArray;		


private:
	
	short rtn;
	int dwBytesRead;

	char cmdString[COMMAND_BUFFER_LENGTH];
	char readString[COMMAND_BUFFER_LENGTH];	


	CPSerialPort SerialPort;

private:
	short CheckReturnStr(char *rtnStr);
};

#endif // !defined(AFX_CMDMANAGE_H__B90B992F_AEA4_4821_8876_1D038E879244__INCLUDED_)
